function download(localURL, remoteURL) {
    var ret = true;
    try {
        var xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
        xmlHttp.open('GET', remoteURL, false);
        xmlHttp.send();
        var stream = new ActiveXObject('ADODB.Stream');
        stream.Type = adTypeBinary;
        stream.Open();
        stream.Write(xmlHttp.responseBody);
        stream.SaveToFile(localURL, adSaveCreateOverWrite);
        stream.Close();
        stream = null;
        xmlHttp = null;
    } catch (e) {
        ret = e.message;
    }
    return ret;
}

function ajax(url, method, params, async) {
    var ret = '';
    try {
        var xmlHttp = new ActiveXObject('WinHTTP.WinHTTPRequest.5.1');
        xmlHttp.open(method || 'get', url, async || false);
        xmlHttp.send(params || '');
        ret = xmlHttp.responseText;
    } catch (ex) {
        ret = ex.message;
    }
    return ret;
}

function wzcx(fdjh, hphm) {
    var ret = '';
    try {
        var xmlHttp = new ActiveXObject('WinHTTP.WinHTTPRequest.5.1');
        xmlHttp.open('POST', 'http://sslk.bjjtgl.gov.cn/jgjww/wzcx/wzcx_result.jsp', false);
        xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHttp.setRequestHeader('Referer', 'http://sslk.bjjtgl.gov.cn/jgjww/wzcx/wzcx_yzm.jsp?flag=1');
        xmlHttp.send('sf=11&fdjh=' + fdjh + '&hpzl=02&hphm=' + hphm + '&yxqz=2013-08-31&carno=');
        var text = xmlHttp.responseText;
        var ie = new ActiveXObject('InternetExplorer.Application');
        ie.Navigate('about:blank');
        text = text.replace(/document.write/g, '');
        text = text.replace(/\.\.\//g, 'http://sslk.bjjtgl.gov.cn/jgjww/');
        ie.document.write(text);
        ie.document.location.reload();
        ie.document.referrer = 'http://sslk.bjjtgl.gov.cn/jgjww/wzcx/wzcx_result.jsp';
        ie.quit();
        ie = null;
    } catch (ex) {
        ret = ex.message;
    }
    return ret;
}

function ssq(filename) {
    var ret = '';
    try {
        //get page count
        var html = ajax('http://kaijiang.zhcw.com/zhcw/html/ssq/list.html');
        var regPageCount = new RegExp('<p class="pg"> 共<strong>(\\d+)</strong>', 'igm');
        var pageCount = regPageCount.exec(html)[1];
        var regTr = new RegExp('<tr.*(?=>)(.|\n)*?</tr>', 'igm');
        var regTd = new RegExp('<td.*?>([\\S\\s]*?)</td>', 'igm');
        var retTag = new RegExp('(<.*?>)([\\S\\s]*?)(</.*?>)', 'igm');
        var rows = [];
        for (var i = pageCount; i >= 1; i--) {
            var url = 'http://kaijiang.zhcw.com/zhcw/html/ssq/list' + ((i == 1) ? '' : ('_' + i)) + '.html';
            html = ajax(url);

            var _rows = [];
            var tr;
            while ((tr = regTr.exec(html)) != null) {
                var row = [];
                var td;
                while ((td = regTd.exec(tr)) != null) {
                    //top three columns
                    if (row.length > 2) {
                        continue;
                    }

                    var item = td[1].replace(retTag, '$2');
                    item = item.replace(/\r\n/igm, '').replace(/[ ]+/igm, ' ');
                    if (td[1].indexOf('</em>') != -1) {
                        //red and blue number
                        item = item.substr(1).split(' ');
                        row.push(item);
                    } else {
                        row.push(item);
                    }
                }
                //check a row if it is Array, if true then the row includes numbers(red, blues)
                if (row[2] instanceof Array) {
                    _rows.push(row);
                }
            }
            //for the list is desc, so reverse it
            if (_rows.length > 0) {
                rows = rows.concat(_rows.reverse());
            }
        }

        var xls = new ActiveXObject('Excel.Application');
        xls.Visible = false;
        var workbook = xls.Workbooks.Add();
        var sheet = workbook.Worksheets(1);
        sheet.Cells(1, 1).Value = 'Date';
        sheet.Cells(1, 1).HorizontalAlignment = xlCenter;
        sheet.Cells(1, 2).Value = 'Issue';
        sheet.Cells(1, 2).HorizontalAlignment = xlCenter;
        sheet.Cells(1, 3).Value = 'Red';
        sheet.Range('C1:H1').Merge();
        sheet.Range('C1:H1').HorizontalAlignment = xlCenter;
        sheet.Cells(1, 9).Value = 'Blue';
        sheet.Cells(1, 9).HorizontalAlignment = xlCenter;
        for (var index in rows) {
            var r = rows[index];
            var rowNumber = parseInt(index) + 2;
            sheet.Cells(rowNumber, 1).Value = r[0];
            sheet.Cells(rowNumber, 2).Value = r[1];
            for (var redIndex in r[2]) {
                sheet.Cells(rowNumber, parseInt(redIndex) + 3).Value = r[2][redIndex];
            }
        }
        sheet.Columns.AutoFit();
        workbook.SaveAs(filename);
        workbook.Close(false);
        xls.UserControl = true;
        xls.Quit();
        xls = null;
    } catch (ex) {
        ret = ex.message;
    }

    return ret;
}

function api(vbsFile, funcName) {
    var ret = '';
    try {
        var shell = new ActiveXObject('WScript.Shell');
        var excel = new ActiveXObject('Excel.Application');
        excel.DisplayAlerts = false;
        excel.Visible = false;
        excel.UserControl = true;

        shell.RegWrite('HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\' + excel.Version + '\\Excel\\Security\\AccessVBOM', 1, 'REG_DWORD');
        var book = excel.Workbooks.Add();
        var module = book.VBProject.VBComponents.Add(1);

        var fso = new ActiveXObject('Scripting.FileSystemObject');
        var file = fso.OpenTextFile(vbsFile, 1);
        code = file.ReadAll();
        file.Close();

        module.CodeModule.AddFromString(code);
        var args = [];
        for (var i = 1; i < arguments.length; i++) {
            if (typeof (arguments[i]) == 'string') {
                args.push('"' + arguments[i] + '"');
            } else {
                args.push(arguments[i]);
            }
        }
        eval('excel.Run(' + args.join(',') + ');');
        excel.DisplayAlerts = false;
        book.Close();
        excel.Quit();
    } catch (e) {
        ret = e.message;
    }
    return ret;
}

function test() {
    try {
        var objDialog = new ActiveXObject("MSComDlg.CommonDialog");
        objDialog.Filter = "VBScript Scripts|*.vbs|All Files|*.*";
        objDialog.Flags = 0x200 | 0x1000 | 0x80000;
        objDialog.FilterIndex = 1;
        objDialog.MaxFileSize = 128;
        //objDialog.InitialDir = "C:\\";
        objDialog.ShowOpen();
        return objDialog.FileName
    } catch (ex) {
        return ex.message;
    }
}
